<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 门店管理
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;

class Mendian extends Common
{
	//列表
    public function index(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = input('param.field').' '.input('param.order');
			}else{
				$order = 'sort desc,id';
			}
			$where = [];
			$where[] = ['aid','=',aid];
			$where[] = ['bid','=',bid];
			if(input('param.name')) $where[] = ['name','like','%'.input('param.name').'%'];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['status','=',input('param.status')];
			$count = 0 + Db::name('mendian')->where($where)->count();
			$data = Db::name('mendian')->where($where)->page($page,$limit)->order($order)->select()->toArray();
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}
        return View::fetch();
    }
	//编辑
	public function edit(){
		if(input('param.id')){
			$info = Db::name('mendian')->where('aid',aid)->where('bid',bid)->where('id',input('param.id/d'))->find();
			}else{
			$info = array('id'=>'');
            }
		View::assign('bid',bid);
		View::assign('info',$info);
        return View::fetch();
	}
	public function save(){
		$info = input('post.info/a');

		if($info['latitude']){
			//通过坐标获取省市区
			$url = 'https://apis.map.qq.com/ws/geocoder/v1/?key=ABLBZ-4BIKU-GFTVB-BK7IK-OLQ35-QCBFF&location='.$info['latitude'].','.$info['longitude'];
			$res = json_decode(request_get($url),true);
			//dump($res);
			if($res && $res['status']==0){
				$address_component = $res['result']['address_component'];
				$info['area'] = $address_component['province'].','.$address_component['city'].','.$address_component['district'];
					$info['province'] = $address_component['province'];
					$info['city']     = $address_component['city'];
					$info['district'] = $address_component['district'];
			}
		}
		if($info['id']){
			Db::name('mendian')->where('aid',aid)->where('bid',bid)->where('id',$info['id'])->update($info);
			\app\common\System::plog('编辑门店'.$info['id']);
		}else{
            $info['aid'] = aid;
			$info['bid'] = bid;
			$info['createtime'] = time();
			$id = Db::name('mendian')->insertGetId($info);
			\app\common\System::plog('添加门店'.$id);
		}
		return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index')]);
	}
	//改状态
	public function setst(){
		$st = input('post.st/d');
		$ids = input('post.ids/a');
		Db::name('mendian')->where('aid',aid)->where('bid',bid)->where('id','in',$ids)->update(['status'=>$st]);
		\app\common\System::plog('修改门店状态'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//删除
	public function del(){
		$ids = input('post.ids/a');
		Db::name('mendian')->where('aid',aid)->where('bid',bid)->where('id','in',$ids)->delete();
		\app\common\System::plog('删除门店'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}
	//列表
    public function chooseindex(){
		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = input('param.field').' '.input('param.order');
			}else{
				$order = 'sort desc,id';
			}
			$where = [];
			$where[] = ['aid','=',aid];
			if(bid==0){
				$bids = Db::name('business')->where('aid',aid)->where('isplatform_auth',1)->where('status',1)->column('id');	
				array_push($bids,bid);
				$where[] = ['bid','in',$bids];
			}else{
				$where[] = ['bid','=',bid];
			}
			if(input('param.name')) $where[] = ['name','like','%'.input('param.name').'%'];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['status','=',input('param.status')];
			$count = 0 + Db::name('mendian')->where($where)->count();
			$data = Db::name('mendian')->where($where)->page($page,$limit)->order($order)->select()->toArray();
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}

		return View::fetch();
    }
	public function choosemendian(){
        if(request()->isPost()){
            $id = input('param.id/d');
            $mendian = Db::name('mendian')->where('aid',aid)->where('id',$id)->find();
            return json(['status'=>1,'data'=>$mendian]);
        }
        return View::fetch();
    }

	public function getpaycode(){
		if(request()->isPost()){
            $id = input('param.id/d');
			$bid = input('param.bid/d');
			$type = input('param.type');
			$set = 	Db::name('admin_set')->field('paycode_bgpic')->where('aid',aid)->find();
			$page = 'pages/maidan/pay';
			$scene = 'mdid_'.$id.'-'.'bid_'.$bid;
			//var_dump($scene);
			$mendian = Db::name('mendian')->field('paycodepic')->where('aid',aid)->where('id',$id)->update(['paycodepic'=>$posterurl]);
			if(!$mendian['paycodepic']){
				$posterurl = $this->_getposter(aid,$type,$set['paycode_bgpic'],$page,$scene);
				if($posterurl){
					Db::name('mendian')->where('aid',aid)->where('id',$id)->update(['paycodepic'=>$posterurl]);
				}
			}else{
				$posterurl = $mendian['paycodepic'];
			}
            return json(['status'=>1,'paycodepic'=>$posterurl]);
        }
	}

	//生成付款码
	function _getposter($aid,$type,$poster_bg,$page,$scene){
		set_time_limit(0);
		@ini_set('memory_limit', -1);
		if(strpos($poster_bg,'http') ===false){
			$poster_bg = PRE_URL.$poster_bg;
		}
		$bg = imagecreatefromstring(request_get($poster_bg));
		if($bg){
			$bgwidth = imagesx($bg);
			$bgheight = imagesy($bg);
			if($bgheight/$bgwidth > 1.92) $bgheight = floor($bgwidth * 1.92);
			$target = imagecreatetruecolor($bgwidth, $bgheight);
			imagecopy($target, $bg, 0, 0, 0, 0,$bgwidth,$bgheight);
			imagedestroy($bg);
		}else{
			$bgwidth = 680;
			$bgheight = 1080;
			$target = imagecreatetruecolor(680, 1080);
			imagefill($target,0,0,imagecolorallocate($target, 255, 255, 255));
		}
		$huansuan = $bgwidth/340;
		//$bgwidth = imagesx($bg);
		//$bgheight = imagesy($bg);
		$font = ROOT_PATH."static/fonts/msyh.ttf";
		
		$qrwidth = $bgwidth/2;
		$qrleft = $bgwidth/4;
		$qrtop = $bgheight/2 - $qrwidth/2;
		if ($type == 'qrwx'){
			$url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.\app\common\Wechat::access_token($aid,'wx');
			$data = array();
			$data['scene'] = $scene;
			$data['page'] = ltrim($page,'/');
			$res = request_post($url,jsonEncode($data));
			$errmsg =json_decode($res,true);
			
			if($errmsg){
				if($errmsg['errcode'] == 41030){
					echojson(array('status'=>0,'msg'=>'小程序发布后才能生成分享海报'));
				}else{
					echojson(array('status'=>0,'msg'=>$errmsg['errmsg']));
				}
			}
			if(!$errmsg){
				$img = imagecreatefromstring($res);
				imagecopyresampled($target, $img, $qrleft, $qrtop, 0, 0,$qrwidth,$qrwidth,imagesx($img), imagesy($img));
			}else{
				//echojson(array('status'=>0,'msg'=>$errmsg['errmsg']));
			}
		} else if ($type == 'qrmp') {
			$qrcode = createqrcode(PRE_URL .'/h5/'.$aid.'.html#'.$page.'?scene='.$scene.'&t='.time());
			$img = imagecreatefromstring(request_get($qrcode));
			imagecopyresampled($target, $img, $qrleft, $qrtop, 0, 0, $qrwidth,$qrwidth,imagesx($img), imagesy($img));
		}
	
		$url = '/upload/'.date('Ym/d_His').rand(1000,9999).'.jpg';
		$filepath = ROOT_PATH.ltrim($url,'/');
		mk_dir(dirname($filepath));
		imagejpeg($target,$filepath,100);
		return PRE_URL.$url;
	}
}
